package com.sun.xacml.attr;

import java.net.URI;
import java.util.List;

import org.jdom.Namespace;
import org.jdom.input.DOMBuilder;
import org.w3c.dom.Element;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class PolygonAttribute extends AttributeValue {
	private static URI identifierURI;
	private static String srsName;
	private static Polygon geometry;
	
	/**
	 * Constructor tao polygon attribute tu jdomRoot co cau truc <Polygon></Polygon>
	 */
	public PolygonAttribute(org.jdom.Element jdomRoot) {
		super(identifierURI);

		Namespace gml = null;
		String strShell = "";
		String strHoles = "";
		String str = "";
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsName
		srsName = jdomRoot.getAttributeValue("srsName");

		/* Shell of Polygons */
		org.jdom.Element shell = jdomRoot.getChild("outerBoundaryIs", gml);
		org.jdom.Element Linear = shell.getChild("LinearRing", gml);
		org.jdom.Element coord = Linear.getChild("coord", gml);
		if (coord != null) {
			List<org.jdom.Element> coordList = Linear.getChildren("coord", gml);
			for (int i = 0; i < coordList.size(); i++) {
				String X = "";
				String Y = "";
				X = coordList.get(i).getChild("X", gml).getValue();
				Y = coordList.get(i).getChild("Y", gml).getValue();
				strShell = strShell + X + " " + Y;
				if (i != coordList.size() - 1)
					strShell = strShell + ",";
			}
		} else 	strShell = Linear.getChild("coordinates", gml).getValue();
			
		/* Holes of Polygon */
		List<org.jdom.Element> holesList = jdomRoot.getChildren("innerBoundaryIs",gml);
		
		//neu polygon co holes
		if(holesList.size() != 0)
		{
			strHoles = ",";
			for(int j = 0;j < holesList.size(); j++)
			{
				strHoles = strHoles + "(" ;
				org.jdom.Element Linear1 = holesList.get(j).getChild("LinearRing", gml);
				org.jdom.Element coord1 = Linear1.getChild("coord", gml);
				if (coord1 != null) {
					List<org.jdom.Element> coordList = Linear1.getChildren("coord", gml);
					for (int k = 0; k < coordList.size(); k++) {
						String X = "";
						String Y = "";
						X = coordList.get(k).getChild("X", gml).getValue();
						Y = coordList.get(k).getChild("Y", gml).getValue();
						strHoles = strHoles + X + " " + Y;
						if (k != coordList.size() - 1) strHoles = strHoles + ",";
					}
				} else 	strHoles = strHoles + Linear1.getChild("coordinates", gml).getValue();
				strHoles += ")";
				if(j != holesList.size()-1) strHoles = strHoles + ",";
			}
		}

		/* Ghep chuoi shell va holes thanh chuoi Polygon */
		str = "(" + strShell + ")" + strHoles;
		
		//tao gia tri jts
		try {		
			geometry = (Polygon) new WKTReader().read("POLYGON(" + str + ")");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Constructor tao polygon attribute tu DOMROOT
	 */
	public PolygonAttribute(Element domRoot){
		super(identifierURI);

		DOMBuilder domBuilder = new DOMBuilder();
		org.jdom.Element jdomRoot = domBuilder.build(domRoot);
		
		Namespace gml = null;
		String strShell = "";
		String strHoles = "";
		String str = "";
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsName
		srsName = jdomRoot.getAttributeValue("srsName");

		/* Shell of Polygons */
		org.jdom.Element shell = jdomRoot.getChild("outerBoundaryIs", gml);
		org.jdom.Element Linear = shell.getChild("LinearRing", gml);
		org.jdom.Element coord = Linear.getChild("coord", gml);
		if (coord != null) {
			List<org.jdom.Element> coordList = Linear.getChildren("coord", gml);
			for (int i = 0; i < coordList.size(); i++) {
				String X = "";
				String Y = "";
				X = coordList.get(i).getChild("X", gml).getValue();
				Y = coordList.get(i).getChild("Y", gml).getValue();
				strShell = strShell + X + " " + Y;
				if (i != coordList.size() - 1)
					strShell = strShell + ",";
			}
		} else	strShell = Linear.getChild("coordinates", gml).getValue();
			
		/* Holes of Polygon */
		List<org.jdom.Element> holesList = jdomRoot.getChildren("innerBoundaryIs",gml);
		
		//neu polygon co holes thi tao chuoi strHoles
		if(holesList.size() != 0)
		{
			strHoles = ",";
			for(int j = 0;j < holesList.size(); j++)
			{
				strHoles = strHoles + "(" ;
				org.jdom.Element Linear1 = holesList.get(j).getChild("LinearRing", gml);
				org.jdom.Element coord1 = Linear1.getChild("coord", gml);
				if (coord1 != null) {
					List<org.jdom.Element> coordList = Linear1.getChildren("coord", gml);
					for (int k = 0; k < coordList.size(); k++) {
						String X = "";
						String Y = "";
						X = coordList.get(k).getChild("X", gml).getValue();
						Y = coordList.get(k).getChild("Y", gml).getValue();
						strHoles = strHoles + X + " " + Y;
						if (k != coordList.size() - 1) strHoles = strHoles + ",";
					}
				} else 	strHoles = strHoles + Linear1.getChild("coordinates", gml).getValue();
				strHoles += ")";
				if(j != holesList.size()-1) strHoles = strHoles + ",";
			}
		}

		/* Ghep chuoi shell va holes thanh chuoi Polygon */
		str = "(" + strShell + ")" + strHoles;
		
		//tao gia tri jts
		try {			
			geometry = (Polygon)new WKTReader().read("POLYGON(" + str + ")");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//get geometry
	public Polygon getGeometry() {
		return geometry;
	}
	
	//get srsName
	public String getSrsName() {
		return srsName;
	}

	@Override
	public String encode() {
		// TODO Auto-generated method stub
		return geometry.toString();
	}

	public static AttributeValue getInstance(org.jdom.Element root)
			throws ParseException {
		// TODO Auto-generated method stub
		return getInstance((Element) root);
	}

	public static AttributeValue getInstance(Element root)
			throws ParseException {
		// TODO Auto-generated method stub
		return new PolygonAttribute(root);
	}
}
